package com.kwan.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.databinding.BindingAdapter;
import androidx.databinding.InverseBindingAdapter;
import androidx.databinding.InverseBindingListener;


public class CustomEditView extends LinearLayout implements View.OnClickListener {

	private Context mContext;

	private ImageView iv_left;
	private EditText et_content;
	private ImageView iv_btn_clear, iv_btn_display;
	private boolean isDisplayPsw = false;
	private View layout_focus;

	private int ic_select, ic_no_select;
	private boolean is_password;
	private String hintStr, textStr;
	private CustomListener listener;

	public CustomEditView(Context context) {
		super(context);
		mContext = context;
		init();
	}

	public CustomEditView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		init(attrs);
	}

	public CustomEditView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		mContext = context;
		init(attrs);
	}

	public void setCustomListener(CustomListener listener) {
		this.listener = listener;
	}

	public String getTextVal() {
		return et_content.getText().toString();
	}

	private void init(AttributeSet attrs) {
		TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CustomEditView);
		ic_select = typedArray.getResourceId(R.styleable.CustomEditView_ic_select, 0);
		ic_no_select = typedArray.getResourceId(R.styleable.CustomEditView_ic_no_select, 0);
		is_password = typedArray.getBoolean(R.styleable.CustomEditView_is_password, false);
		hintStr = typedArray.getString(R.styleable.CustomEditView_str_hint);
		textStr = typedArray.getString(R.styleable.CustomEditView_str_text);
		typedArray.recycle();
		init();
	}

	private void init() {
		View view = inflate(mContext, R.layout.custom_edit_view, this);
		layout_focus = view.findViewById(R.id.layout_focus);
		iv_left = view.findViewById(R.id.iv_left);
		iv_left.setVisibility((ic_select == 0 && ic_no_select == 0) ? View.GONE : View.VISIBLE);
		setLeftImage(false);
		et_content = view.findViewById(R.id.et_content);
		iv_btn_clear = view.findViewById(R.id.iv_btn_clear);
		iv_btn_clear.setOnClickListener(this);
		iv_btn_display = view.findViewById(R.id.iv_btn_display);
		iv_btn_display.setOnClickListener(this);

		if (is_password) {
			iv_btn_display.setVisibility(View.VISIBLE);
			setIvBtnDisplay(isDisplayPsw);
		} else {
			et_content.setInputType(InputType.TYPE_CLASS_TEXT);
			iv_btn_display.setVisibility(View.GONE);
		}

		et_content.setHint(hintStr);
		//et_content.setText(textStr);

		iv_btn_clear.setVisibility(View.GONE);

		et_content.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				iv_btn_clear.setVisibility(TextUtils.isEmpty(s.toString()) ? View.GONE : View.VISIBLE);
				if (listener != null) {
					listener.onEditIsNull(!TextUtils.isEmpty(s.toString()));
				}
			}
		});


		et_content.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				setLeftImage(hasFocus);
				if (listener != null) {
					listener.onHasFocus(hasFocus);
				}
			}
		});
	}

	private void setLeftImage(boolean hasFocus) {
		if (iv_left.getVisibility() == View.VISIBLE) {
			iv_left.setImageResource(hasFocus ? ic_select : ic_no_select);
		}
		layout_focus.setVisibility(hasFocus ? View.VISIBLE : View.GONE);
	}

	private void setIvBtnDisplay(boolean isDisplayPsw) {
		iv_btn_display.setImageResource(isDisplayPsw ? R.mipmap.icon_psw_hide : R.mipmap.icon_psw_display);
		et_content.setTransformationMethod(isDisplayPsw ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
	}

	public void setText(CharSequence text) {
		if (et_content != null) {
			et_content.setText(text);
		}
	}

	@Override
	public void onClick(View v) {

		if (v == iv_btn_clear) {
			et_content.setText("");
		} else if (v == iv_btn_display) {
			isDisplayPsw = !isDisplayPsw;
			setIvBtnDisplay(isDisplayPsw);
		}
	}

	public interface CustomListener {
		void onEditIsNull(boolean isNull);

		void onHasFocus(boolean hasFocus);
	}


	@BindingAdapter("str_text")
	public static void setContent(CustomEditView cev, String content) {
		String mCurrentStr = cev.et_content.getText().toString().trim();
		if (!TextUtils.isEmpty(content)) {
			if (!content.equalsIgnoreCase(mCurrentStr)) {
				cev.et_content.setText(content);
				// 设置光标位置
				cev.et_content.setSelection(content.length());
			}
		}
	}


	// GET 方法
	@InverseBindingAdapter(attribute = "str_text", event = "CevContentAttrChanged")
	public static String getContent(CustomEditView cev) {
		return cev.et_content.getText().toString().trim();
	}


	// 监听,如果有变动就调用listener中的onChange方法
	@BindingAdapter(value = "CevContentAttrChanged")
	public static void setChangeListener(CustomEditView cev,
										 InverseBindingListener listener) {
		cev.et_content.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				listener.onChange();
			}

			@Override
			public void afterTextChanged(Editable s) {


			}
		});
	}


}
